package com.citcc.web.controller.business;

import com.citcc.business.bean.*;
import com.citcc.business.service.*;
import com.citcc.common.annotation.Log;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.enums.BusinessType;
import com.citcc.common.page.TableDataInfo;
import com.citcc.common.utils.DateUtils;
import com.citcc.common.utils.poi.ExcelUtil;
import com.citcc.framework.web.base.BaseController;
import com.citcc.system.bean.SysUser;
import com.citcc.web.controller.util.ResponseResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/monitorCenter")
public class MonitorCenterController extends BaseController {

    @Autowired
    private MonitorCenterService monitorCenterService;
    @Autowired
    private CdzStationService cdzStationService;
    @Autowired
    private CdzChargingService chargingService;

    private String prefix = "business/monitorCenter";

    @RequiresPermissions("business:monitorCenter:view")
    @GetMapping("/view")
    public String vehicle() {
        return prefix + "/view";
    }

    @GetMapping("/timeWeather")
    public String timeWeather() {
        return prefix + "/timeWeather";
    }
    /**
     * 获取车辆树
     * @param request
     * @param response
     * @param model
     * @return
     */@RequestMapping(value = "/getVehicleTree")
    @ResponseBody
    public Object getVehicleTree(HttpServletRequest request, HttpServletResponse response, ModelMap model) {

        SysUser user = getSysUser();
        String orgId = user.getDeptId();
        List<Map<String, String>> orgList = monitorCenterService.getOrgTree(orgId);
        List<Map<String, String>> list = monitorCenterService.getStationList(orgId);
        List<Map<String, String>> listNew =new ArrayList<>();
        for (Map<String, String> map : list) {
            String alarmCount="0";
			/*for (Map<String, Object> alarmMap : alarmList) {
				if(String.valueOf(alarmMap.get("NAME")).equals(map.get("NAME"))){
					alarmCount=String.valueOf(alarmMap.get("COUNT"));
				}
			}*/
            Map<String, String> mapSon=new HashMap<>();
            String id = map.get("ID");
            String pid = map.get("PID");
            String name = map.get("NAME");
            String longitude = map.get("LONGITUDE");
            String latitude = map.get("LATITUDE");
            String type=map.get("DLTYPE");
            if(type.equals("1")){
                mapSon.put("type","直流");
                Map<String, String> mapp=monitorCenterService.getAllCount(id,"1");
                mapSon.put("zlstakeCount",mapp.get("stakeCount"));
                mapSon.put("zlgunCount",mapp.get("gunCount"));
                mapSon.put("zlonline",mapp.get("online"));
                mapSon.put("jlstakeCount","0");
                mapSon.put("jlgunCount","0");
                mapSon.put("jlonline","0");
            }else if(type.equals("2")){
                mapSon.put("type","交流");
                Map<String, String> mapp=monitorCenterService.getAllCount(id,"2");
                mapSon.put("jlstakeCount",mapp.get("stakeCount"));
                mapSon.put("jlgunCount",mapp.get("gunCount"));
                mapSon.put("jlonline",mapp.get("online"));
                mapSon.put("zlstakeCount","0");
                mapSon.put("zlgunCount","0");
                mapSon.put("zlonline","0");
            }else if(type.equals("3")){
                mapSon.put("type","混合");
                Map<String, String> mapp=monitorCenterService.getAllCount(id,"1");
                mapSon.put("zlstakeCount",mapp.get("stakeCount"));
                mapSon.put("zlgunCount",mapp.get("gunCount"));
                mapSon.put("zlonline",mapp.get("online"));
                Map<String, String> map2=monitorCenterService.getAllCount(id,"2");
                mapSon.put("jlstakeCount",map2.get("stakeCount"));
                mapSon.put("jlgunCount",map2.get("gunCount"));
                mapSon.put("jlonline",map2.get("online"));
            }
            mapSon.put("ID", id);
            mapSon.put("PID", pid);
            mapSon.put("NAME", name);
            mapSon.put("l", longitude+","+latitude);
            mapSon.put("count", alarmCount);
            listNew.add(mapSon);

        }
        orgList.addAll(listNew);
        return orgList;
    }

    /**
     * 根据车辆id获取车辆的弹窗信息
     * @param request
     * @param response
     * @param model
     * @param id
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/getBusPopInfo")
    @ResponseBody
    public ResponseResult getBusPopInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, String id) throws ParseException {
        Map<String, String> jo = new HashMap<>();
        Map<String, String> map = monitorCenterService.getGDInfoById(id);
        String ago = DateUtils.get3MinAgo();
        jo.put("id",map.get("ID"));
        jo.put("name",map.get("NAME"));
        jo.put("address",map.get("ADDRESS"));
        jo.put("num",map.get("NUM"));
        jo.put("lat",map.get("LATITUDE"));
        jo.put("lng",map.get("LONGITUDE"));
        jo.put("line","1");
        jo.put("businessTime",map.get("BUSINESS_TIME"));
        if(map.get("TYPE").equals("1")){
            jo.put("type","直流");
            Map<String, String> mapp=monitorCenterService.getAllCount(id,"1");
            jo.put("zlstakeCount",mapp.get("stakeCount"));
            jo.put("zlgunCount",mapp.get("gunCount"));
            jo.put("zlonline",mapp.get("online"));
            jo.put("jlstakeCount","0");
            jo.put("jlgunCount","0");
            jo.put("jlonline","0");
        }else if(map.get("TYPE").equals("2")){
            jo.put("type","交流");
            Map<String, String> mapp=monitorCenterService.getAllCount(id,"2");
            jo.put("jlstakeCount",mapp.get("stakeCount"));
            jo.put("jlgunCount",mapp.get("gunCount"));
            jo.put("jlonline",mapp.get("online"));
            jo.put("zlstakeCount","0");
            jo.put("zlgunCount","0");
            jo.put("zlonline","0");
        }else if(map.get("TYPE").equals("3")){
            jo.put("type","混合");
            Map<String, String> mapp=monitorCenterService.getAllCount(id,"1");
            jo.put("zlstakeCount",mapp.get("stakeCount"));
            jo.put("zlgunCount",mapp.get("gunCount"));
            jo.put("zlonline",mapp.get("online"));
            Map<String, String> map2=monitorCenterService.getAllCount(id,"2");
            jo.put("jlstakeCount",map2.get("stakeCount"));
            jo.put("jlgunCount",map2.get("gunCount"));
            jo.put("jlonline",map2.get("online"));
        }
        return new ResponseResult(jo);

    }

    /**
     * 实时监控列表
     * @return
     */
    @RequiresPermissions("business:monitorCenter:main")
    @GetMapping("/main")
    public String main(ModelMap map) {
        CdzStation station = new CdzStation();
        station.setOrgId(getSysUser().getDeptId());
        map.put("station",cdzStationService.selectCdzStationAll(station));
        return prefix + "/main";
    }

    /**
     * 实时监控列表查询
     */
    @RequiresPermissions("business:monitorCenter:order")
    @GetMapping("/order")
    @ResponseBody
    public TableDataInfo order(Monitor monitor){
        startPage();
        List<Monitor> list=monitorCenterService.queryByList(monitor);
        return getDataTable(list);
    }

    /**
     * 实时监控导出
     */
    @RequiresPermissions("business:monitorCenter:export")
    @Log(title = "实时监控导出", businessType = BusinessType.UPDATE)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Monitor monitor)
    {
        List<Monitor> list=monitorCenterService.queryByList(monitor);
        ExcelUtil<Monitor> util = new ExcelUtil<Monitor>(Monitor.class);
        return util.exportExcel(list, DateUtils.dateTimeNow()+"实时监控数据");
    }

    /**
     * 展示充电信息
     */
    @GetMapping("/toChargeList/{orderId}")
    public String toGunList(@PathVariable("orderId")String orderId, ModelMap map)
    {
        map.put("orderId",orderId);
        return prefix + "/chargeList";
    }

    /**
     * 获取单个充电信息
     */
    @RequestMapping(value = "/getCharge")
    @ResponseBody
    public ResponseResult getCharge(String id){
        CdzChargingEntity charge=chargingService.selectCdzChargingEntityById(id);
        return new ResponseResult(charge);
    }

}
